
package cn.tang.tframe.sdm.aop.handler;

import cn.tang.tframe.common.exception.ValidateUtil;
import cn.tang.tframe.sdm.aop.ApiCheckApp;
import cn.tang.tframe.sdm.aop.WrapAspectUtil;
import cn.tang.tframe.sdm.domain.ClientParam;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.Method;

/**
 * Created by tanghc on 17/1/18.
 */
public class ServiceApiCheckAppPreHandler extends PreHandler {
	@Override
	protected boolean doPreHandler(
		ProceedingJoinPoint pjp, Object[] args, Method method,
		String className) {

		ApiCheckApp annotation = WrapAspectUtil.findAnnotation(
				pjp.getTarget().getClass(), method, ApiCheckApp.class);
		if(null != annotation){
			ClientParam clientParam = retriveUcParam(args);
			ValidateUtil.checkNotNull(clientParam, "参数不能为空");
			ValidateUtil.checkNotEmpty(clientParam.getProductCode(), "productCode不能为空");
			ValidateUtil.checkNotEmpty(clientParam.getAppCode(), "appCode不能为空");
		}
		return true;
	}
	protected ClientParam retriveUcParam(Object[] args){
		if(null != args){
			for(Object o:args){
				if(o instanceof ClientParam){
					return (ClientParam)o;
				}
			}
		}
		return null;
	}
}
